/**
 * 
 */
package com.edocbase.desktop.plugins.control;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

import com.edocbase.desktop.lightweight.widgets.TabItem;
import com.edocbase.desktop.swt.widgets.RichBrowser;

/**
 * @author X61
 * 
 */
public class HTMLRichViewer implements Listener {
private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger
		.getLogger(HTMLRichViewer.class);
	private RichBrowser richBrowser = null;
	private TabItem item = null;
	private IShortcutKeyControl shortcutControl = null;

	public HTMLRichViewer(Composite parent, int style) {
		richBrowser = new RichBrowser(parent, style);
		richBrowser.addListener(SWT.KeyDown, this);
	}

	public IShortcutKeyControl getShortcutControl() {
		return shortcutControl;
	}

	public void setShortcutControl(IShortcutKeyControl shortcutControl) {
		this.shortcutControl = shortcutControl;
	}

	public RichBrowser getRichBrowser() {
		return richBrowser;
	}

	public void open(String url) {
		richBrowser.setUrl(url);
	}

	public boolean getDesignMode() {
		return richBrowser.getDesignMode();
	}

	public void setDesignMode(boolean design) {
		richBrowser.setDesignMode(design);
	}

	public void dispose() {
		if (richBrowser != null && !richBrowser.isDisposed()) {
			richBrowser.dispose();
			richBrowser = null;
		}
		if (item != null) {
			item = null;
		}
	}

	public TabItem getItem() {
		return item;
	}

	public void setItem(TabItem item) {
		this.item = item;
	}

	@Override
	public void handleEvent(Event event) {
		switch (event.type) {
		case SWT.KeyDown:
		case SWT.KeyUp:
			if (event.stateMask == SWT.CTRL) {
				if (event.keyCode == 'S' || event.keyCode == 's') {
					if (shortcutControl != null) {
						shortcutControl.save();
						break;
					}
				}
			}
			if (event.keyCode == SWT.CTRL || event.keyCode == SWT.ALT
					|| event.keyCode == SWT.SHIFT
					|| event.keyCode == SWT.CAPS_LOCK ) {
				break;
			}
			if (shortcutControl != null) {
				shortcutControl.editing();
			}
			break;
		}
	}

}
